<template>
  <el-dialog
    :title="!dataForm.id ? '新增' : '修改'"
    :close-on-click-modal="false"
    :visible.sync="visible">
    <el-form :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmit()" label-width="80px">
    <el-form-item label="商家" prop="sellerId">
      <el-input v-model="dataForm.sellerId" placeholder="商家"></el-input>
    </el-form-item>
    <el-form-item label="更新日期" prop="updateTime">
      <el-input v-model="dataForm.updateTime" placeholder="更新日期"></el-input>
    </el-form-item>
    <el-form-item label="自动夜审(1启用0禁用)" prop="autoCheck">
      <el-input v-model="dataForm.autoCheck" placeholder="自动夜审(1启用0禁用)"></el-input>
    </el-form-item>
    <el-form-item label="最后一次夜审时间" prop="lastNightTrialTime">
      <el-input v-model="dataForm.lastNightTrialTime" placeholder="最后一次夜审时间"></el-input>
    </el-form-item>
    <el-form-item label="夜审时间设置" prop="setNightTrialTime">
      <el-input v-model="dataForm.setNightTrialTime" placeholder="夜审时间设置"></el-input>
    </el-form-item>
    <el-form-item label="手动(1是0禁用)" prop="lastNightState">
      <el-input v-model="dataForm.lastNightState" placeholder="手动(1是0禁用)"></el-input>
    </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
  export default {
    data () {
      return {
        visible: false,
        dataForm: {
          id: 0,
          sellerId: '',
          updateTime: '',
          autoCheck: '',
          lastNightTrialTime: '',
          setNightTrialTime: '',
          lastNightState: ''
        },
        dataRule: {
          sellerId: [
            { required: true, message: '商家不能为空', trigger: 'blur' }
          ],
          updateTime: [
            { required: true, message: '更新日期不能为空', trigger: 'blur' }
          ],
          autoCheck: [
            { required: true, message: '自动夜审(1启用0禁用)不能为空', trigger: 'blur' }
          ],
          lastNightTrialTime: [
            { required: true, message: '最后一次夜审时间不能为空', trigger: 'blur' }
          ],
          setNightTrialTime: [
            { required: true, message: '夜审时间设置不能为空', trigger: 'blur' }
          ],
          lastNightState: [
            { required: true, message: '手动(1是0禁用)不能为空', trigger: 'blur' }
          ]
        }
      }
    },
    methods: {
      init (id) {
        this.dataForm.id = id || 0
        this.visible = true
        this.$nextTick(() => {
          this.$refs['dataForm'].resetFields()
          if (this.dataForm.id) {
            this.$http({
              url: this.$http.adornUrl(`/hotel/thotelnighttrial/info/${this.dataForm.id}`),
              method: 'get',
              params: this.$http.adornParams()
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.dataForm.sellerId = data.tHotelNightTrial.sellerId
                this.dataForm.updateTime = data.tHotelNightTrial.updateTime
                this.dataForm.autoCheck = data.tHotelNightTrial.autoCheck
                this.dataForm.lastNightTrialTime = data.tHotelNightTrial.lastNightTrialTime
                this.dataForm.setNightTrialTime = data.tHotelNightTrial.setNightTrialTime
                this.dataForm.lastNightState = data.tHotelNightTrial.lastNightState
              }
            })
          }
        })
      },
      // 表单提交
      dataFormSubmit () {
        this.$refs['dataForm'].validate((valid) => {
          if (valid) {
            this.$http({
              url: this.$http.adornUrl(`/hotel/thotelnighttrial/${!this.dataForm.id ? 'save' : 'update'}`),
              method: 'post',
              data: this.$http.adornData({
                'id': this.dataForm.id || undefined,
                'sellerId': this.dataForm.sellerId,
                'updateTime': this.dataForm.updateTime,
                'autoCheck': this.dataForm.autoCheck,
                'lastNightTrialTime': this.dataForm.lastNightTrialTime,
                'setNightTrialTime': this.dataForm.setNightTrialTime,
                'lastNightState': this.dataForm.lastNightState
              })
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.$message({
                  message: '操作成功',
                  type: 'success',
                  duration: 1500,
                  onClose: () => {
                    this.visible = false
                    this.$emit('refreshDataList')
                  }
                })
              } else {
                this.$message.error(data.msg)
              }
            })
          }
        })
      }
    }
  }
</script>
